/**
 * 输入若干个文本框，将其中的value格式化，也就是将空格/tab位/回车/左括号/右括号替换成html的专用编码
 * @param {Object} textarea1
 * @param {Object} textarea2
 * @param {Object} textarea3
 * @return {TypeName} 
 */

function formatText(textarea1, textarea2, textarea3) {

	var format = /([\s|\n|<|>])/g;
	var result = textarea1.value;
	result=ubbtohtml(result);
	textarea1.value = result;

	var result = textarea2.value;
	result=ubbtohtml(result);
	textarea2.value = result;
	
	var result = textarea3.value;
	result=ubbtohtml(result);
	textarea3.value = result;

	return true;
}

/**
 * 这里用了一些ubb的代码，用户可以自行定义文章的排版格式
 * @param {Object} str
 * @return {TypeName} 
 */

function ubbtohtml(str) {

	str = str.replace(/</ig, '&lt;');
	str = str.replace(/>/ig, '&gt;');
	str = str.replace(/\n/ig, '<br />');
	str = str.replace(/\[code\](.+?)\[\/code\]/ig, function($1, $2) {
		return phpcode($2);
	});

	str = str.replace(/\[hr\]/ig, '<hr />');
	str = str.replace(/\[\/(size|color|font|backcolor)\]/ig, '</font>');
	str = str.replace(/\[(sub|sup|u|i|strike|b|blockquote|li)\]/ig, '<$1>');
	str = str.replace(/\[\/(sub|sup|u|i|strike|b|blockquote|li)\]/ig, '</$1>');
	str = str.replace(/\[\/align\]/ig, '</p>');
	str = str.replace(/\[(\/)?h([1-6])\]/ig, '<$1h$2>');

	str = str.replace(/\[align=(left|center|right|justify)\]/ig,
			'<p align="$1">');
	str = str.replace(/\[size=(\d+?)\]/ig, '<font size="$1">');
	str = str.replace(/\[color=([^\[\<]+?)\]/ig, '<font color="$1">');
	str = str.replace(/\[backcolor=([^\[\<]+?)\]/ig,
			'<font style="background-color:$1">');
	str = str.replace(/\[font=([^\[\<]+?)\]/ig, '<font face="$1">');
	str = str.replace(/\[list=(a|A|1)\](.+?)\[\/list\]/ig,
			'<ol type="$1">$2</ol>');
	str = str.replace(/\[(\/)?list\]/ig, '<$1ul>');

	str = str.replace(/\[s:(\d+)\]/ig, function($1, $2) {
		return smilepath($2);
	});
	str = str.replace(/\[img\]([^\[]*)\[\/img\]/ig,
			'<img src="$1" border="0" />');
	str = str.replace(/\[url=([^\]]+)\]([^\[]+)\[\/url\]/ig,
			'<a href="$1">' + '$2' + '</a>');
	str = str.replace(/\[url\]([^\[]+)\[\/url\]/ig,
			'<a href="$1">' + '$1' + '</a>');
	return str;
}

